package com.netx.data;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.SQLException;


public class DataSourceSink implements DataSink {

	private final DataSource _ds;
	private final PrintWriter _logger;
	
	public DataSourceSink(DataSource ds, PrintWriter out) {
		if(ds == null) {
			throw new IllegalArgumentException("null datasource");
		}
		if(out == null) {
			throw new IllegalArgumentException("null print writer");
		}
		_ds = ds;
		_logger = out;
	}

	public DataSourceSink(DataSource ds) {
		this(ds, new PrintWriter(System.out));
	}
	
	public void update(String query) {
		try {
			java.sql.Connection c = _ds.getConnection();
			c.createStatement().executeUpdate(query);
			c.close();
		}
		catch(SQLException sqle) {
			_logger.println("SQLException in DataSourceSink, query: "+query);
			sqle.printStackTrace(_logger);
		}
	}
}
